/**
 * 
 */
package org.jms.examples.activemqexamples.pubsub;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;

public class SubscriberOne {
	private TopicConnectionFactory factory = null;
	private TopicConnection connection = null;
	private TopicSession session = null;
	private TopicSubscriber subscriber = null;

	public void receiveMessage() {

		try {
			factory = new ActiveMQConnectionFactory(
					ActiveMQConnectionFactory.DEFAULT_BROKER_URL);
			ActiveMQTopic topic = new ActiveMQTopic("topic1");
			connection = factory.createTopicConnection();
			session = connection.createTopicSession(false,
					TopicSession.AUTO_ACKNOWLEDGE);
			subscriber = session.createSubscriber(topic);
			connection.start();
			Message message = subscriber.receive();
			if (message instanceof ObjectMessage) {
				Object object = ((ObjectMessage) message).getObject();
				System.out.println(this.getClass().getName()
						+ " has received a message : " + (EventMessage) object);
			}

		} catch (JMSException e) {

			e.printStackTrace();
		} finally {

			if (connection != null) {
				try {
					connection.close();
				} catch (JMSException ex) {
					ex.printStackTrace();
				}
			}
		}

	}

	public static void main(String[] args) {
		SubscriberOne subscriberOne = new SubscriberOne();
		subscriberOne.receiveMessage();

	}

}
